<?php

/**
 * login actions.
 *
 * @package    tinystockmis
 * @subpackage login
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class tsmGuardAuthActions extends sfActions
{
    /**
     * //display the login form
     *
     * @param sfRequest $request A request object
     */

    public function preExecute()
    {
        //fetch the account set
        //$this->acct_sets = Doctrine::getTable("AcctSet")->getAllActiveSets();

    }
    
    //public function executeIndex(sfWebRequest $request)
    //{

        #$this->forward('default', 'module');
        //if has signed, forward to the main page
        /*if($this->getUser()->isAuthenticated())  {
            $this->redirect('/');
        }*/
        //$this->forwardIf($this->getUser()->isAuthenticated(), "main", "index");
        //$this->redirectIf($this->getUser()->isAuthenticated(), "@homepage");
        //show the login form
        //$this->login_form = new LoginForm();

    //}


    public function executeSignIn(sfWebRequest $request)
    {
        
        $this->redirectIf($this->getUser()->isAuthenticated(), "@homepage");
        $this->login_form = new LoginForm();

        if($request->isMethod(sfRequest::POST))
        {
            $this->forward404Unless($request->isMethod(sfRequest::POST));
            //$request->checkCSRFProtection();

            //$this->login_form = new LoginForm();

            $this->processForm($request, $this->login_form);

            //print_r($this->login_form->getName());
            $this->getUser()->setFlash("errormsg", "您的用户名和密码不正确，请重试.");
            //$this->setTemplate("index");
        }
    }

    public function executeSignOut(sfWebRequest $request)
    {
        $this->getUser()->setAuthenticated(false);
        $this->getUser()->clearCredentials();
        $this->redirect("@homepage");
    }
    
    public function executeSecure()
    {
        //if not login redirect to the login page
        $this->forwardUnless($this->getUser()->isAuthenticated(), "tsmGuardAuth", "signIn");
        $this->getResponse()->setStatusCode(403);
    }


    protected function processForm(sfWebRequest $request, LoginForm $login_form)
    {
        $login_form->bind( $request->getParameter($login_form->getName()) );
        
        if($login_form->isValid())
        {
            $this->status = "is valid";
            //set the default account set
            $this->getUser()->setAttribute("acct_set", $login_form["acctset"]->getValue());
            //check username and password
            //
            //echo $login_form["username"]->getValue();
            //echo $this->getUser()->getAttribute("acct_set");
            $user = Doctrine::getTable("HrUsers")->findUserByUsername($login_form["username"]->getValue());
            //echo $user->getUserPassword();
            //echo $login_form['password']->getValue();
            
            if(is_object($user) and $user->getUserPassword() == sha1($login_form["password"]->getValue())) {
                $this->getUser()->setAttribute("user_id", $user->getId());
                $this->getUser()->setAttribute("user_code", $user->getUserCode());
                $this->getUser()->setAttribute("user_name", $user->getUserName());
                $this->getUser()->setAttribute("user_fullname", $user->getFullname());
                $this->getUser()->setAuthenticated(true);
                
                //get user credentials
                /*$credentials = Doctrine::getTable("HrUsers")->getUserCredentials($user->getId());
                $this->getUser()->addCredentials($credentials);*/
                $this->getUser()->addCredentials(Doctrine::getTable("HrUsers")->getUserCredentials($user->getId()));
                //print_r($credentials);
                $this->getUser()->setCulture($login_form["lang"]->getValue());
                //redirect to homepage
                $this->redirect("@homepage");
                //return sfView::NONE;
            }
        }

        return false;
        
    }

}
